package control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import util.Adresse;
import util.FormParser;
import util.Geburtstag;
import util.Termin;
import gui.config.ErrorMessage;
import gui.config.NewEvent;

import xml.XMLWorker;

public class AddEvent implements ActionListener {
	
	private XMLWorker worker = null;
	private NewEvent input = null;
	private SimpleDateFormat time = null;
	private SimpleDateFormat date = null;
	
	public AddEvent(XMLWorker worker, NewEvent input) {
		this.worker = worker;
		this.input = input;
		time = new SimpleDateFormat("HH:mm");
		date = new SimpleDateFormat("dd.MM.yyyy");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(inputIsValid()) {
			if(input.isBday()) {
				// Geburtstag wird eingetragen
				addBday();
			} else if(input.isEvent()) {
				// Termin wird eingetragen
				addEvent();
			}
		}
	}
	
	protected boolean inputIsValid() {
		if(input.isBday()) {
			// Geburtstag
			if(FormParser.isDate(input.getDateOfBirthday()) &&
						FormParser.isName(input.getNameOfBirthday()) &&
						FormParser.isComment(input.getCommentOfBirthday())) {
				if(input.hasAdress()) {
					if(FormParser.isStreet(input.getStreetOfBirthday()) &&
							FormParser.isPlace(input.getPlaceOfBirthday()) &&
							FormParser.isCountry(input.getCountryOfBirthday()) &&
							FormParser.isNumber(input.getNumberOfBirthday())) {
						return true;
					} else {
						return false;
					}
				}
				return true;
			}
			return false;
		} else if(input.isEvent()) {
			// Termin
			if(FormParser.isDate(input.getDateOfEvent()) &&
					FormParser.isTime(input.getTimeOfEvent()) &&
					FormParser.isTitle(input.getTitleOfEvent()) &&
					FormParser.isPlace(input.getPlaceOfEvent()) &&
					FormParser.isComment(input.getCommentOfEvent())) {
				return true;
			}
			return false;
		}
		return false;
	}
	
	private void addBday() {
		Geburtstag g = new Geburtstag();
		try {
			g.setDatum(date.parse(input.getDateOfBirthday()));
		} catch (ParseException e1) {
			e1.printStackTrace();
			System.out.println("Use date format: dd.MM.yyyy");
		}
		g.setKommentar(input.getCommentOfBirthday());
		g.setName(input.getNameOfBirthday());
		if(input.hasAdress()) {
			Adresse a = new Adresse();
			a.setStrasse(input.getStreetOfBirthday());
			a.setNummer(Integer.parseInt(input.getNumberOfBirthday()));
			a.setOrt(input.getPlaceOfBirthday());
			a.setLand(input.getCountryOfBirthday());
			g.setAdresse(a);
		}
		worker.add(g);
		input.removeWindow();
	}
	
	private void addEvent() {
		Termin t = new Termin();
		try {
			t.setDatum(date.parse(input.getDateOfEvent()));
			t.setUhrzeit(time.parse(input.getTimeOfEvent()));
		} catch (ParseException e1) {
			e1.printStackTrace();
			System.out.println("Use date/time format: dd.MM.yyyy and HH:mm");
		}
		t.setKommentar(input.getCommentOfEvent());
		t.setTitel(input.getTitleOfEvent());
		t.setOrt(input.getPlaceOfEvent());
		worker.add(t);
		input.removeWindow();
	}
}
